package jukebox.model.qnaboard;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jukebox.model.common.CommonConstants;
import jukebox.model.common.ListVO;
import jukebox.model.common.PagingBean;
import jukebox.model.qnacomment.QnACommentDAO;
import jukebox.model.qnacomment.QnACommentVO;

import org.springframework.stereotype.Service;

@Service
public class QnABoardServiceImpl implements QnABoardService{
	@Resource(name="qnABoardDAOImpl")
	private QnABoardDAO qnaboardDAO;
	@Resource(name="qnACommentDAOImpl")
	private QnACommentDAO qnacommentDAO;
	

	@Override
	public int writeContent(QnABoardVO bvo) {
		if(bvo.getKind()==null||bvo.getKind()=="")
			bvo.setKind("미분류");
		qnaboardDAO.writeContent(bvo);
		return qnaboardDAO.getCurrentNo();
	}

	@Override
	public void updateContent(QnABoardVO bvo) {
		qnaboardDAO.updateContent(bvo);
	}

	@Override
	public void deleteContent(int no) {
		qnaboardDAO.deleteContent(no);
	}
	
	@Override
	public ListVO<QnABoardVO> pagingContent(Integer nowPage, String title) {
		if(nowPage==null)
			nowPage = 1;
		if(title==null)
			title = "";
		int totalContents = qnaboardDAO.getContentCount(title);
		PagingBean pb = new PagingBean(totalContents, nowPage, title);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PAGE", nowPage);
		map.put("CONTENT_NUMBER_PER_PAGE", CommonConstants.CONTENT_NUMBER_PER_PAGE);
		map.put("TITLE", title);
		List<QnABoardVO> list = qnaboardDAO.getContentListPerPage(map);
		Iterator<QnABoardVO> it = list.iterator();
		while(it.hasNext()){
			QnABoardVO qbvo = it.next();
			if(qnacommentDAO.hasBeenChoiced(qbvo.getNo())>0)
				qbvo.setAdopted(true);
		}
		
		return new ListVO<QnABoardVO>(list, pb);
	}
	
	@Override
	public QnABoardVO findContentByNo(int no) {
		QnABoardVO qbvo = qnaboardDAO.findContentByNo(no);
		if(qnacommentDAO.hasBeenChoiced(no)>0)
			qbvo.setAdopted(true);
		return qbvo;
	}
	@Override
	public QnABoardVO findContentByNoIncHit(int no) {
		qnaboardDAO.IncreaseHit(no);
		QnABoardVO qbvo = qnaboardDAO.findContentByNo(no);
		if(qnacommentDAO.hasBeenChoiced(no)>0)
			qbvo.setAdopted(true);
		return qbvo;
	}
	@Override
	public QnABoardVO findContentByNoToModify(int no) {
		QnABoardVO vo = qnaboardDAO.findContentByNo(no);
		return vo;
	}

	@Override
	public ListVO<QnACommentVO> writeComment(QnACommentVO qcvo) {
		qnacommentDAO.writeComment(qcvo);
		System.out.println(qcvo);
		double lastPage = Math.ceil((double)qnacommentDAO.getCountComment(qcvo.getBoardNo())/CommonConstants.CONTENT_NUMBER_PER_PAGE);
		return findPagedCommentListByBoardNo(qcvo.getBoardNo(), (int)lastPage);
	}

	@Override
	public ListVO<QnACommentVO> modifyComment(QnACommentVO qcvo, Integer page) {
		if(page==null)
			page = 1;
		qnacommentDAO.updateComment(qcvo);
		return findPagedCommentListByBoardNo(qcvo.getBoardNo(), page);//수정뒤에도 해당 댓글의 페이지를 가져와서 매개변수에 추가해야 한다.
		
	}

	@Override
	public ListVO<QnACommentVO> deleteComment(int no, int boardNo) {
		qnacommentDAO.deleteComment(no);
		return findPagedCommentListByBoardNo(boardNo, 1);
	}

	@Override
	public ListVO<QnACommentVO> choiceComment(int commentNo, int boardNo, Integer page) {
		if(page==null)
			page = 1;
		qnacommentDAO.choiceComment(commentNo);
		return findPagedCommentListByBoardNo(boardNo, page);
	}

	@Override
	public List<QnACommentVO> findCommentListByBoardNo(int boardNo) {
		return qnacommentDAO.findCommentListByBoardNo(boardNo);
	}
	
	@Override
	public ListVO<QnACommentVO> findPagedCommentListByBoardNo(int boardNo, Integer page) {
		if(page==null)
			page = 1;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("BOARD_NO", boardNo);
		map.put("CONTENT_NUMBER_PER_PAGE", CommonConstants.CONTENT_NUMBER_PER_PAGE);
		map.put("PAGE", page);
		List<QnACommentVO> list = qnacommentDAO.findPagedCommentListByBoardNo(map);
		PagingBean pb = new PagingBean(qnacommentDAO.getCountComment(boardNo), page);
		return new ListVO<QnACommentVO>(list, pb);
	}


}
